package com.mcflies.aotoPartsManager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.mcflies.aotoPartsManager.dao.CustomerAreaDAO;
import com.mcflies.aotoPartsManager.pojo.CustomerArea;
import com.mcflies.aotoPartsManager.service.CustomerAreaService;
import com.mcflies.aotoPartsManager.util.AutoPartsDAOFactory;
import com.mcflies.aotoPartsManager.util.AutoPartsGUIDef;
import com.mcflies.commons.util.ChineseToPinYinUtil;

public class CustomerAreaServiceImpl implements CustomerAreaService {
	private Logger log = Logger.getLogger(CustomerAreaServiceImpl.class);

	@Override
	public Map<String, Object> addCustomerArea(CustomerArea area) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			CustomerAreaDAO areaDAO = AutoPartsDAOFactory.getCustomerAreaDAO();
			String areaShortName = ChineseToPinYinUtil.getChineseToPinYinHeadChar(area.getAreaName());
			area.setAreaShortName(areaShortName);
			if (null == area.getRemark() || "".equals(area.getRemark().trim())) {
				area.setRemark(area.getProvinceName() + "-" + area.getRegionName() + "-" + area.getAreaName());
			}
			CustomerArea customerArea = areaDAO.addCustomerArea(area);
			retMap.put(AutoPartsGUIDef.AreaGuiDef.areaNo, customerArea.getAreaNo());
			retMap.put(AutoPartsGUIDef.AreaGuiDef.areaShortName, customerArea.getAreaShortName());
			retMap.put(AutoPartsGUIDef.AreaGuiDef.areaName, customerArea.getAreaName());
			retMap.put(AutoPartsGUIDef.AreaGuiDef.provinceName, customerArea.getProvinceName());
			retMap.put(AutoPartsGUIDef.AreaGuiDef.regionName, customerArea.getRegionName());
			retMap.put(AutoPartsGUIDef.AreaGuiDef.remark, customerArea.getRemark());
			return retMap;
		} catch (Exception e) {
			log.info("在[" + CustomerAreaServiceImpl.class.getName() + "]执行方法(addCustomerArea)出现：：" + e.getMessage()
					+ "异常");
			throw new Exception("AutoParts~~新增客户地域信息异常");
		}
	}

	@Override
	public JSONObject loadCustomerAreaWithPage(int start, int limit) throws Exception {
		JSONObject retJsonObj = new JSONObject();
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			CustomerAreaDAO areaDAO = AutoPartsDAOFactory.getCustomerAreaDAO();
			Long count = areaDAO.countCustomerArea();
			List<CustomerArea> list = areaDAO.loadCustomerArea(start, limit);

			List<Object> rows = new ArrayList<Object>();
			if (null != list && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					CustomerArea area = list.get(i);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put(AutoPartsGUIDef.AreaGuiDef.areaNo, area.getAreaNo());
					map.put(AutoPartsGUIDef.AreaGuiDef.areaShortName, area.getAreaShortName());
					map.put(AutoPartsGUIDef.AreaGuiDef.areaName, area.getAreaName());
					map.put(AutoPartsGUIDef.AreaGuiDef.provinceName, area.getProvinceName());
					map.put(AutoPartsGUIDef.AreaGuiDef.regionName, area.getRegionName());
					map.put(AutoPartsGUIDef.AreaGuiDef.remark, area.getRemark());
					rows.add(map);
				}
			}
			retMap.put(AutoPartsGUIDef.CommonGuiDef.results, count);
			retMap.put(AutoPartsGUIDef.CommonGuiDef.rows, rows);
		} catch (Exception e) {
			log.info("在[" + CustomerAreaServiceImpl.class.getName() + "]执行方法(loadCustomerAreaWithPage)出现：："
					+ e.getMessage() + "异常");
		}
		retJsonObj = JSONObject.fromObject(retMap);
		return retJsonObj;
	}

}
